<template>
  <div class="login-container">
    <img src="../../assets/bg.jpg" alt="" style="width: 100%;height: 100%;position: absolute">
    <div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <img src="../../assets/loginbg.png" alt="" style="width: 100%;height: 100%;position: absolute;left: 0;top: 0;z-index: 0">
        <div style="position: relative">
          <el-form-item prop="username">
        <span class="svg-container svg-container_login">
            <img src="../../assets/username_icon.png" alt="">
        </span>
            <el-input v-model="loginForm.userName" name="username" type="text" auto-complete="off" placeholder="请输入用户名" />
          </el-form-item>

          <el-form-item prop="password">
        <span class="svg-container">
          <img src="../../assets/password_icon.png" alt="">
        </span>
            <el-input :type="passwordType" v-model="loginForm.password" name="password" auto-complete="off" placeholder="请输入密码" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
          </el-form-item>
          <el-form-item prop="password">
        <span class="svg-container">
          <img src="../../assets/code_icon.png" alt="">
        </span>
            <el-input type="text" v-model="loginForm.checkpic" name="checkpic" auto-complete="off" placeholder="请输入图像验证码" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="changePic">
              <img :src="checkpicSRC" style="width: 100%;height: 35px;">
            </span>
          </el-form-item>
          <el-form-item style="border:0;padding-bottom: 30px;background: none">
        <span class="show-forget" @click="phone">
          <a>忘记密码？</a>
        </span>
          </el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;background-color:#5AA4E9;height: 44px;line-height: 25px;font-size: 18px " @click.native.prevent="handleLogin">登录</el-button>
        </div>
      </el-form>
    </div>
   <div style=" margin-top: 3%;">
     <img src="../../assets/logo.png" alt="" style="position: relative; width:4%; height:4%; left: 5%;">
     <img src="../../assets/xh.png" alt="" style="position: relative; left: 6%; padding-bottom: 0.5%">
   </div>
    <p style="color: #fff;position: relative; left: 5%; color: #2B7CC2; font-size: 16px;font-weight:bold;">区 块 链 保 险 金 服</p>
    <div class="dialog-footer">
      <p class="dialog-font">
        <span>Copyright&nbsp;&copy;&nbsp;&nbsp;{{date}}&nbsp;&nbsp;享和宁杰金融信息服务（上海）有限公司&nbsp;&nbsp;版权所有&nbsp;&nbsp;</span>
        <a href="http://www.beian.miit.gov.cn/" target="_blank">沪ICP备19047252号</a>
      </p>
    </div>
  </div>
</template>

<script>
  import { doGet } from '@/api/index'
  export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else {
        callback()
      }
    }
    return {
      date: '',
      timer: null,
      checkpicSRC:"",
      loginForm: {
        userName: '',
        password: '',
        checkpic: ''
      },
      loginRules: {
        userName: [{ required: true, message: '管理员账户不允许为空', trigger: 'blur' }],
        password: [
          { required: true, message: '管理员密码不允许为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }

  },
  created() {
    this.changePic();
    this.nowTimes();
  },
  beforeDestroy: function() {
    if (this.timer) {
      clearTimeout(this.timer); //在Vue实例销毁前，清除我们的定时器
    }
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    changePic(){
      doGet("checkpic.html?name=login",'').then((response)=>{
        this.checkpicSRC="data:image/png;base64,"+response
        console.log("susccess"+response)
      }).catch((error)=>{
        this.checkpicSRC="data:image/png;base64,"+error
        console.log("error==>"+JSON.stringify(error))
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
            this.loading = false
            this.changePic()
          })
        } else {
          return false
        }
      })
    },
    phone(){
      this.$router.push({ path: '/resetPassword'})
    },
    timeFormate(timeStamp) {
      let year = new Date(timeStamp).getFullYear();
      // return year + "年" + month + "月" + date +"日"+" "+hh+":"+mm ;
      this.date = year ;
    },
    nowTimes() {
      this.timeFormate(new Date());
      this.timer = setTimeout(this.nowTimes, 30 * 1000);
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    right: 50px;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 190px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd ,.show-forget {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #fff;
    cursor: pointer;
    user-select: none;
  }
  .show-forget {
    color: #2B7CC2;
    &:hover {
      color: #FFFFFF;
      border-bottom: 1px solid #FFFFFF;
    }
  }
  .dialog-footer {
    margin: 0;
    color: #fff;
    font-size: 16px;
    position: relative;
    left: 30%;
    top: 77%;
    .dialog-font {
    a {
      &:hover {
        color: #FFFFFF;
        border-bottom: 1px solid #FFFFFF;
      }
    }
    }
  }
}
</style>
